package server;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import common.VSM_AboutScreen;
import common.VSM_ProjectInfo;

public class VSM_Monitor extends JFrame implements ActionListener, Runnable
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5178902125289760564L;
	private final JMenuBar menuBar = new JMenuBar();
	private final JMenu menuFile = new JMenu("File");
	private final JMenu menuHelp = new JMenu("Help");
	
	private final JMenuItem menuItemExit = new JMenuItem("Exit");
	private final JMenuItem menuItemHelp = new JMenuItem("Help Contents");
	private final JMenuItem menuItemAbout = new JMenuItem("About");
	
	private final JPanel backPanel = new JPanel();
	
	private final JScrollPane scrollPanel = new JScrollPane(backPanel);
	
	private boolean isRunning = false;
	
	VSM_Monitor()
	{
		this.setTitle(VSM_ProjectInfo.getProjectname() + " - Monitor (" + VSM_ProjectInfo.getProjectversion() + ")");
		this.setSize(800,400);
		this.getContentPane().setBackground(Color.darkGray);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setLayout(new BorderLayout(5,5));
		
		this.setJMenuBar(menuBar);
		
		this.add(scrollPanel,BorderLayout.CENTER);
		
		backPanel.setBackground(Color.lightGray);
		
		menuBar.add(menuFile);
		menuBar.add(menuHelp);
		
		menuFile.add(menuItemExit);
		
		menuHelp.add(menuItemHelp);
		menuHelp.add(menuItemAbout);
		
		menuItemExit.addActionListener(this);
		
		menuItemAbout.addActionListener(this);
		
		startMonitor();
	}
	
	void startMonitor()
	{
		Thread playThread = new Thread(this);
		isRunning = true;
		playThread.start();
	}

	@Override
	public void run()
	{
		ServerSocket serverSocket = null;
		try
		{
			serverSocket = new ServerSocket(2048);
			
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try
		{
			Socket socket = null;
			
			while(isRunning == true)
			{
				socket = serverSocket.accept();
				
				new VSM_ClientHandler(socket, this);
			}
			    
		} catch (IOException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		finally
		{
			try
			{
				serverSocket.close();
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		if(arg0.getSource() == menuItemExit)
		{
			this.dispose();
		}
		
		else if(arg0.getSource() == menuItemAbout)
		{
			VSM_AboutScreen aboutWindow = new VSM_AboutScreen(this,VSM_ProjectInfo.getProjectname() + " (" + VSM_ProjectInfo.getProjectversion() + ") - About");
			aboutWindow.setModal(true);
			aboutWindow.setVisible(true);
		}
		
		backPanel.repaint();

	}
	
	void addNewDataDisplay(VSM_DataDisplay dataDisplay)
	{
		backPanel.add(dataDisplay);
	}
	
	void removeDataDisplay(VSM_DataDisplay dataDisplay)
	{
		backPanel.remove(dataDisplay);
	}

}
